package org.report4service.utils;

import javax.microedition.lcdui.Alert;
import javax.microedition.lcdui.AlertType;
import javax.microedition.lcdui.Command;
import javax.microedition.lcdui.CommandListener;
import javax.microedition.lcdui.Display;
import javax.microedition.lcdui.Displayable;

import org.report4service.listeners.AlertMessageListener;

/**
 * Displays alert messages to the user
 * 
 * @author Jmpango
 * 
 */
public class AlertMessage implements CommandListener {

    private Display display;
    private Displayable prevScreen;
    private boolean alertMode = false;
    private String title;
    private AlertMessageListener listener;

    public AlertMessage(Display display, String title, Displayable prevScreen,
	    AlertMessageListener listener) {
	setDisplay(display);
	setTitle(title);
	setPrevScreen(prevScreen);
	setListener(listener);
    }

    /**
     * Display a confirmation message.
     * 
     * @param message
     *            - the message text.
     */
    public void showConfirm(String message) {
	Alert alert = new Alert(title, message, null, AlertType.INFO);
	alert.setTimeout(Alert.FOREVER);
	alert.addCommand(CommandPool.btnYes);
	alert.addCommand(CommandPool.btnNo);
	alert.setCommandListener(this);
	display.setCurrent(alert, prevScreen);
	alertMode = true;
    }

    /**
     * Displays a message.
     * 
     * @param message
     *            - the actual message text.
     */
    public void show(String message) {
	show(message, AlertType.INFO);
    }

    /**
     * Displays a message.
     * 
     * @param message
     *            - the actual message text.
     * @param alertType
     *            - the type of message. eg erro, info, etc.
     */
    public void show(String message, AlertType alertType) {
	Alert alert = new Alert(title, message, null, alertType);
	alert.setTimeout(Alert.FOREVER);
	alert.addCommand(CommandPool.btnOk);
	alert.setCommandListener(this);
	display.setCurrent(alert, prevScreen);
	alertMode = true;
    }

    /**
     * Processes the command events.
     * 
     * @param c
     *            - the issued command.
     * @param d
     *            - the screen object the command was issued for.
     */
    public void commandAction(Command c, Displayable d) {
	try {
	    if (c == CommandPool.btnOk || c == CommandPool.btnYes
		   )
		handleOkCommand(d);
	    else if (c == CommandPool.btnNo)
		handleCancelCommand(d);
	} catch (Exception e) {
	}

	this.alertMode = false;
    }

    /**
     * Processes the cancel command event.
     * 
     * @param d
     *            - the screen object the command was issued for.
     */
    private void handleCancelCommand(Displayable d) {
	if (this.listener != null)
	    this.listener.onAlertMessage(AlertMessageListener.MSG_CANCEL);
    }

    /**
     * Processes the OK command event.
     * 
     * @param d
     *            - the screen object the command was issued for.
     */
    private void handleOkCommand(Displayable d) {
	if (this.listener != null)
	    this.listener.onAlertMessage(AlertMessageListener.MSG_OK);
    }

    /** Turns off the current alert. */
    public void turnOffAlert() {
	display.setCurrent(prevScreen);
    }

    public Display getDisplay() {
	return display;
    }

    public void setDisplay(Display display) {
	this.display = display;
    }

    public AlertMessageListener getListener() {
	return listener;
    }

    public void setListener(AlertMessageListener listener) {
	this.listener = listener;
    }

    public Displayable getPrevScreen() {
	return prevScreen;
    }

    public void setPrevScreen(Displayable prevScreen) {
	this.prevScreen = prevScreen;
    }

    public String getTitle() {
	return title;
    }

    public void setTitle(String title) {
	this.title = title;
    }

    public void setAlertMode(boolean alertMode) {
	this.alertMode = alertMode;
    }
}
